package com.wwu.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wwu.common.constant.CommConst;
import com.wwu.common.enums.MenuBtnEnum;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.common.aspect.Log;
import com.wwu.system.entity.SysMenu;
import com.wwu.system.service.ISysMenuService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 *  系统菜单前端控制器
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Api(tags = "系统菜单控制器(sysMenuController)")
@ApiSort(3)
@Controller
@RequestMapping("/system/sysMenu")
public class SysMenuController {
    @Resource
    private ISysMenuService sysMenuService;

    /**
     * 请求转发菜单管理主页面
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/2 16:31
     */
    @GetMapping("/menuMain")
    public String menuMainHtml() {
        return "system/sysmenu/menu-main";
    }

    /**
     * 请求系统菜单新增或更新页面
     * @param menuId
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/12 20:10
     */
    @GetMapping("/menuAddOrUpdate")
    public String menuAddOrUpdateHtml(String menuId,Model model){
        if(StringUtils.hasText(menuId)){
            model.addAttribute(CommConst.SYS_MENU,sysMenuService.getById(menuId));
        }
        return "system/sysmenu/menu-add-edit";
    }

    @Log(menuName = "菜单管理",menuBtnEnum = MenuBtnEnum.SHOW)
    @ApiOperation(value = "查询菜单信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "sysMenu", value = "菜单实体")
    })
    @GetMapping("/menuList")
    @ResponseBody
    public ResultPage<SysMenu> menuList(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "5000") Integer limit,
                                        SysMenu sysMenu){
        return sysMenuService.getMenuListPage(page,limit,sysMenu);
    }

    @Log(menuName = "菜单管理",menuBtnEnum = MenuBtnEnum.SHOW)
    @ApiOperation(value = "根据菜单id查询菜单的父及同级菜单树")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单Id"),
            @ApiImplicitParam(name = "menuType", value = "菜单类型(0:模块;1:目录;2:菜单;3:按钮)",defaultValue = "2")
    })
    @GetMapping("/parentMenuTree")
    @ResponseBody
    public ResultObj parentMenuTree(String menuId,Integer menuType){
        return sysMenuService.parentMenuTree(menuId,menuType);
    }

    @Log(menuName = "菜单管理",menuBtnEnum = MenuBtnEnum.ADD)
    @ApiOperation(value = "添加菜单信息")
    @ApiOperationSupport(order = 3)
    @PostMapping("/saveMenu")
    @ResponseBody
    public  ResultObj saveMenu(@RequestBody JSONObject sysMenuJson){
        sysMenuService.saveMenu(sysMenuJson);
        return ResultObj.success("菜单信息添加成功");
    }

    @Log(menuName = "菜单管理",menuBtnEnum = MenuBtnEnum.EDIT)
    @ApiOperation(value = "更新菜单信息")
    @ApiOperationSupport(order = 4)
    @PutMapping("/updateMenu")
    @ResponseBody
    public ResultObj updateMenu(@RequestBody SysMenu sysMenu){
        sysMenuService.updateMenu(sysMenu);
        return ResultObj.success("菜单信息更新成功");
    }

    @Log(menuName = "菜单管理",menuBtnEnum = MenuBtnEnum.STATUS)
    @ApiOperation(value = "更新菜单状态")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单Id", required = true),
            @ApiImplicitParam(name = "menuStatus", value = "菜单状态(0:启用;1:作废)",required = true)
    })
    @PutMapping("/updateMenuStatus")
    @ResponseBody
    public ResultObj updateMenuStatus(String menuId,Integer menuStatus){
        sysMenuService.updateMenuStatus(menuId,menuStatus);
        return ResultObj.success("更新菜单状态成功");
    }

    @Log(menuName = "菜单管理",menuBtnEnum = MenuBtnEnum.DEL)
    @ApiOperation(value = "删除菜单信息")
    @ApiOperationSupport(order = 6)
    @DeleteMapping("/deleteMenu/{menuIds}")
    @ResponseBody
    public ResultObj deleteMenu(@ApiParam(name = "menuIds", value = "菜单id集合，以逗号分隔", required = true)
                                @PathVariable String menuIds){
        sysMenuService.deleteMenu(Arrays.asList(menuIds.split(CommConst.SPLIT_COMMA)));
        return ResultObj.success("菜单信息删除成功");
    }
}